package com.mrdeer.web.service.sys.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mrdeer.common.entity.beans.PageEntity;
import com.mrdeer.common.entity.dto.MessageSendDTO;
import com.mrdeer.common.entity.sys.MessageSend;
import com.mrdeer.common.exception.BusinessException;
import com.mrdeer.web.mapper.MessageSendMapper;
import com.mrdeer.web.service.sys.SysMessageSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Slf4j
@Service
@Transactional
public class SysMessageSendServiceImpl extends ServiceImpl<MessageSendMapper, MessageSend> implements SysMessageSendService {

    @Resource
    private MessageSendMapper messageSendMapper;

    @Resource
    private SimpMessagingTemplate messagingTemplate;

    @Override
    public MessageSend send(MessageSend messageSend) {
        MessageSend ms = add(messageSend);
        messagingTemplate.convertAndSendToUser(messageSend.getUserId(), "/queue/subscribe", "您收到了新的消息");
        return ms;
    }

    @Override
    public void deleteByMessageId(String messageId) {
        messageSendMapper.deleteById(messageId);
    }

    @Override
    public void updateStatusByUserId(String userId, Integer status) {
        messageSendMapper.updateStatusByUserId(userId, status);
    }

    @Override
    public void deleteByUserId(String userId) {
        messageSendMapper.delete(new LambdaQueryWrapper<MessageSend>().eq(MessageSend::getUserId, userId));
    }

    @Override
    public MessageSend add(MessageSend messageSend) {
        if (messageSendMapper.insert(messageSend) > 0) {
            return messageSendMapper.selectById(messageSend.getId());
        }
        throw new BusinessException("add failed");
    }

    @Override
    public boolean delete(String id) {
        MessageSend messageSend = getById(id);
        if (null == messageSend) {
            throw new BusinessException("The messageSend does not exists");
        }
        return messageSendMapper.deleteById(id) > 0;
    }

    @Override
    public MessageSend update(MessageSend messageSend) {
        if (messageSendMapper.updateById(messageSend) > 0) {
            MessageSend select = messageSendMapper.selectById(messageSend.getId());
            return select;
        }
        throw new BusinessException("update failed");
    }

    @Override
    public MessageSend getById(String id) {
        return messageSendMapper.selectById(id);
    }

    @Override
    public Page<MessageSend> list(MessageSendDTO dto) {
        if (null == dto.getPage()) {
            dto.setPage(new PageEntity());
        }
        return messageSendMapper.selectPage(dto.getPage().convert(), fuzzyQuery(dto));
    }

    @Override
    public LambdaQueryWrapper<MessageSend> fuzzyQuery(MessageSendDTO dto) {
        LambdaQueryWrapper<MessageSend> queryWrapper = Wrappers.<MessageSend>query().lambda();
        if (null != dto) {
            if (StrUtil.isNotBlank(dto.getTitle())) {
                queryWrapper.like(MessageSend::getTitle, dto.getTitle());
            }
            if (StrUtil.isNotBlank(dto.getUserId())) {
                queryWrapper.eq(MessageSend::getUserId, dto.getUserId());
            }
            if (StrUtil.isNotBlank(dto.getMessageId())) {
                queryWrapper.eq(MessageSend::getMessageId, dto.getMessageId());
            }
            if (StrUtil.isNotBlank(dto.getId())) {
                queryWrapper.eq(MessageSend::getId, dto.getId());
            }
            if (StrUtil.isNotBlank(dto.getContent())) {
                queryWrapper.like(MessageSend::getContent, dto.getContent());
            }
        }
        return queryWrapper;
    }
}
